﻿/******************************************************    
文件名:jxzy_clsj.cs
版权  (c)  2010-11-23 淘渺圈圈工作室
作者:黄媛媛，李伟
创建日期:2010-12-07
维护人员:黄媛媛，李伟
维护日期:2010-12-07
文件功能描述:金属材料热处理网络学习平台_教学资源_材料数据
******************************************************/
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Mmht.BT;

public partial class jxzy_clsj : BasePage
{
    GetSteelData gs = new GetSteelData();
    ColumnName cn = new ColumnName();
    DataTable table = new DataTable();
    int table_type;

    public string class1 = "";
    public string class2 = "";
    public string class3 = "";
    public string class4 = "";

    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["type"] == null)
        {
            Response.Redirect("login.aspx");
            return;
        }
        if (!IsPostBack)
        {
            if (Request["table_type"] != null)
            {
                table_type = Convert.ToInt32(Request["table_type"].ToString());
            }
            else
            {
                table_type = 1;
            }
            getTable(table_type);
            getGridView(table_type);
            ViewState["table_type"] = table_type;
            ddlSearchType.SelectedIndex = table_type - 1;
        }
        else
        {
            table_type = (int)ViewState["table_type"];
            table = (DataTable)ViewState["table"];
        }
    }


    private void databind(int table_type)
    {
        GridView1.DataSource = getTable(table_type);
        GridView1.DataBind();
    }
    /// <summary>
    /// 原始table
    /// </summary>
    /// <param name="table_type"></param>
    /// <returns></returns>
    private DataTable getTable(int table_type)
    {
        table = new DataTable();
        switch (table_type)
        {
            case 1: table = gs.get_SteelComposition(); break;
            case 2: table = gs.get_MaterialsNumber(); break;
            case 3: table = gs.get_StructureProperty(); break;
            case 4: table = gs.get_HeatTreatingData(); break;
            default: break;
        }
        if (table == null)
        {
            return null;
        }
        for (int i = 0; i < table.Rows.Count; i++)
        {
            int j = i + 1;
            table.Rows[i][0] = j.ToString();
        }
        ViewState["table"] = table;
        return table;
    }
    /// <summary>
    /// 根据table生成GridView
    /// </summary>
    /// <param name="table_type"></param>
    /// <param name="dt0"></param>
    private void getGridView(int table_type)
    {
        DataTable dt = new DataTable();
        dt = table;
        BoundField bf = new BoundField();
        string datafield = string.Empty;
        string headertext = string.Empty;
        string tablename = string.Empty;
        clearLeft();//左边栏的事儿
        switch (table_type)
        {
            case 1:
                tablename = "steel_composition";
                class1 = "cur";
                break;
            case 2:
                tablename = "materials_number";
                class2 = "cur";
                break;
            case 3:
                tablename = "structure_property";
                class3 = "cur";
                break;
            case 4:
                tablename = "heat_treating_data";
                class4 = "cur";
                break;
            default:
                break;
        }
        if (!IsPostBack)
        {
            Hashtable hs = cn.col_table(tablename);
            for (int i = 0; i < dt.Columns.Count; i++)
            {
                bf = new BoundField();
                datafield = dt.Columns[i].ColumnName;
                if (hs.Contains(datafield))
                {
                    headertext = hs[datafield].ToString();
                }
                else
                {
                    headertext = datafield;
                }
                bf.DataField = datafield;
                bf.HeaderText = headertext;

                if (datafield == "photo" || datafield.Contains("curve") || datafield == "diagram")
                {
                    //GridView1.Columns[i].Visible = false;
                }
                else
                {
                    GridView1.Columns.Add(bf);
                }
            }
        }

        this.setGridviewCol(1);
        GridView1.DataSource = dt;
        GridView1.DataBind();
    }

    private void databind()
    {
        GridView1.DataSource = table;
        GridView1.DataBind();
    }
    /// <summary>
    /// 分页后绑定数据源
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GridView gv = (GridView)sender;
        int newPageIndex = 0;
        if (-2 == e.NewPageIndex)
        {
            System.Web.UI.WebControls.TextBox tb = null;
            GridViewRow gvr = gv.BottomPagerRow;
            if (gvr != null)
            {
                tb = (System.Web.UI.WebControls.TextBox)gvr.FindControl("txtNum");

            }
            if (tb != null)
            {
                newPageIndex = int.Parse(tb.Text) - 1;
            }
        }
        else
        {
            newPageIndex = e.NewPageIndex;
        }
        newPageIndex = newPageIndex <= 0 ? 0 : newPageIndex;
        this.GridView1.PageIndex = newPageIndex;
        //分页后重新绑定数据源

        databind();

    }

    /// <summary>
    /// 行绑定
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowIndex != -1)
        {
            int id = e.Row.RowIndex + 1;
            e.Row.Cells[0].Text = id.ToString();

            if (table_type == 3)
            {
                int row = Convert.ToInt32(e.Row.Cells[0].Text);
                row--;
                string url = table.Rows[row]["photo"].ToString();
                if (url.Length == 0)
                {
                    url = "css/image/chart_line.png";
                }
                //((HyperLink)e.Row.Cells[9].FindControl("photo")).NavigateUrl = url;
                ((HyperLink)e.Row.Cells[1].FindControl("photo")).NavigateUrl = url;
            }
            else if (table_type == 4)
            {
                int row = Convert.ToInt32(e.Row.Cells[0].Text);
                row--;
                string url1 = table.Rows[row]["cct_curve"].ToString();
                string url2 = table.Rows[row]["ttt_curve"].ToString();
                string url3 = table.Rows[row]["jominy_curve"].ToString();
                string url4 = table.Rows[row]["tempering_curve"].ToString();
                string url5 = table.Rows[row]["diagram"].ToString();
                if (url1.Length == 0)
                {
                    url1 = "css/image/chart_line.png";
                }
                if (url2.Length == 0)
                {
                    url2 = "css/image/chart_line.png";
                }
                if (url3.Length == 0)
                {
                    url3 = "css/image/chart_line.png";
                }
                if (url4.Length == 0)
                {
                    url4 = "css/image/chart_line.png";
                }
                if (url5.Length == 0)
                {
                    url5 = "css/image/chart_line.png";
                }
                ((HyperLink)e.Row.Cells[10].FindControl("cct_curve")).NavigateUrl = url1;
                ((HyperLink)e.Row.Cells[11].FindControl("ttt_curve")).NavigateUrl = url2;
                ((HyperLink)e.Row.Cells[12].FindControl("jominy_curve")).NavigateUrl = url3;
                ((HyperLink)e.Row.Cells[13].FindControl("tempering_curve")).NavigateUrl = url4;
                ((HyperLink)e.Row.Cells[14].FindControl("diagram")).NavigateUrl = url5;

            }
        }

    }

    /// <summary>
    /// 初始化左导航栏的样式
    /// </summary>
    private void clearLeft()
    {
        class1 = "";
        class1 = "";
        class1 = "";
        class1 = "";
    }

    /// <summary>
    /// 设置grideview的显示格式
    /// </summary>
    /// <param name="begin"></param>
    private void setGridviewCol(int begin)
    {
        for (int i = begin; i < 7 + begin; i++)
        {
            GridView1.Columns[i].Visible = false;
        }
        switch (table_type)
        {
            case 1:
                GridView1.Width = 1300;
                break;
            case 2:
                GridView1.Width = 800;
                break;
            case 3:
                GridView1.Width = 900;
                GridView1.Columns[begin].Visible = true;
                break;
            case 4:
                GridView1.Width = 1400;
                GridView1.Columns[begin + 1].Visible = true;
                GridView1.Columns[begin + 2].Visible = true;
                GridView1.Columns[begin + 3].Visible = true;
                GridView1.Columns[begin + 4].Visible = true;
                GridView1.Columns[begin + 5].Visible = true;
                break;
        }
    }

    protected void btSearch_Click(object sender, EventArgs e)
    {
        string key = tbSearchValue.Text;
        string table_name = ddlSearchType.SelectedValue.ToString();
        table = gs.search_Steel(table_name, key);
        if (table != null)
        {
            for (int i = 0; i < table.Rows.Count; i++)
            {
                int j = i + 1;
                table.Rows[i][0] = j.ToString();
            }
        }
        ViewState["table"] = table;
        switch (table_name)
        {
            case "steel_composition": table_type = 1; break;
            case "materials_number": table_type = 2; break;
            case "structure_property": table_type = 3; break;
            case "heat_treating_data": table_type = 4; break;
            default: table_type = 1; break;
        }
        getGridView(table_type);
    }
}
